package com.yuanfengerp.supplierexpense.service.impl;

import com.yuanfengerp.customerexpenses.model.CustomerExpensesInfo;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import com.yuanfengerp.supplierexpense.dao.ISupplierExpenseMapper;
import com.yuanfengerp.supplierexpense.model.SupplierExpenseInfo;
import com.yuanfengerp.supplierexpense.model.SupplierExpensesItemsInfo;
import com.yuanfengerp.supplierexpense.service.ISupplierExpenseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SupplierExpenseServiceImpl implements ISupplierExpenseService {
    @Autowired
    private ISupplierExpenseMapper isem;
    SnowflakeIdWorker idWorker = new SnowflakeIdWorker(10);

    @Override
    public Result<String> deleteSupplierExpenseInfo(String supplier_expense_ID) {
        int i = isem.deleteByPrimaryKey(supplier_expense_ID);
        return i > 0 ? Result.success("删除成功") : Result.failure(500, "删除失败");
    }

    @Override
    public Result<String> insertSupplierExpenseInfo(SupplierExpenseInfo supplierExpenseInfo) {

        // 生成供应商费用ID
        supplierExpenseInfo.setSupplier_expense_ID(idWorker.nextId());

        // 设置操作时间和初始状态等信息
        supplierExpenseInfo.setSupplier_expense_operate_time(new Timestamp(System.currentTimeMillis()));
        supplierExpenseInfo.setSupplier_expense_state(0);
        supplierExpenseInfo.setSupplier_expense_review_time(new Timestamp(System.currentTimeMillis()));
        System.out.println(supplierExpenseInfo.toString());

        // 插入供应商费用信息到数据库
        int i = isem.insertSupplierExpenses(supplierExpenseInfo);
        Double totalValue = 0.0;
        if (i > 0) {
            // 如果插入成功，继续处理连表新增的费用项信息插入
            List<SupplierExpensesItemsInfo> supplierExpensesItemsInfos = supplierExpenseInfo.getSupplierExpensesItemsInfos();
            if (supplierExpensesItemsInfos != null && !supplierExpensesItemsInfos.isEmpty()) {
                for (SupplierExpensesItemsInfo supplierExpensesItemsInfo : supplierExpensesItemsInfos) {
                    System.out.println(supplierExpensesItemsInfo.toString());// 插入费用项信息到连接表
                    Double value = supplierExpensesItemsInfo.getValue();
                    totalValue += value;
                    supplierExpensesItemsInfo.setSupplier_expense_ID(supplierExpenseInfo.getSupplier_expense_ID());
                    int j = isem.insertSupplierExpensesItems(supplierExpensesItemsInfo);
                    if (j <= 0) {
                        // 如果插入连接表失败，需要进行相应处理，这里简单返回失败结果
                        return Result.failure(500, "添加费用项到连接表失败");
                    }
                }
            }
            //保留小数点后两位
            DecimalFormat df = new DecimalFormat("#.00");
            String formattedValue = df.format(totalValue);
            double result = Double.parseDouble(formattedValue);
            supplierExpenseInfo.setSupplier_expense_total_price(result);
            int i1 = isem.updateSupplierExpenseInfo(supplierExpenseInfo);
            if (i1 <= 0)
                return Result.failure(500, "更新费用信息失败");
            // 如果所有操作都成功，返回成功结果
            return Result.success("添加成功");
        } else {
            // 如果插入失败，返回失败结果并附带错误信息
            return Result.failure(500, "添加失败，数据库插入操作未成功");
        }

    }

    @Override
    public Result<String> updateSupplierExpenseInfo(SupplierExpenseInfo supplierExpenseInfo) {
        supplierExpenseInfo.setSupplier_expense_review_time(new Timestamp(System.currentTimeMillis()));
        int i = isem.updateSupplierExpenseInfo(supplierExpenseInfo);
        return i > 0 ? Result.success("修改成功") : Result.failure(500, "修改失败");
    }


    @Override
    public Result<List<SupplierExpenseInfo>> selectAllSupplierExpenseInfo() {
        List<SupplierExpenseInfo> supplierExpenseInfos = isem.selectAllSupplierExpenseInfo();
        if (supplierExpenseInfos.isEmpty())
            return Result.failure(400, "没有数据");
        return Result.success(supplierExpenseInfos);
    }

    @Override
    public Result<List<SupplierExpenseInfo>> selectSupplierExpenseInfo(Map<String, Object> params) {
        Logger logger = LoggerFactory.getLogger(this.getClass());
//
//        // 获取分页参数并设置默认值
//        Integer pageSize = (Integer) params.get("pageSize");
//        Integer pageNum = (Integer) params.get("pageNum");
//
//        System.out.println(params.get("pageSize"));
//        System.out.println(params.get("pageNum"));
//        // 计算分页偏移量
//        int offset = (pageNum - 1) * pageSize;
//        params.put("pageSize", pageSize);
//        params.put("offset", offset);
//
//        // 根据分页和条件查询费用项目信息
//        System.out.println(params.get("pageSize"));
//        System.out.println(params.get("offset"));
        System.out.println(params.get("pageNum"));
        System.out.println(params.get("pageSize"));

        params.put("pageNum", ((int) params.get("pageNum") - 1) * (int) params.get("pageSize"));
        List<SupplierExpenseInfo> supplierExpenseInfos = isem.selectSupplierExpenseInfo(params);

        if (supplierExpenseInfos.isEmpty()) {
            logger.warn("未查询到符合条件的数据");
            return Result.failure(400, "没有数据");
        } else {
            logger.info("成功查询到 {} 条费用项目信息", supplierExpenseInfos.size());
            return Result.success(supplierExpenseInfos);
        }
    }

    @Override
    public Result<List<SupplierExpenseInfo>> selectSupplierExpenseInfoItems(String supplier_expense_ID) {
        List<SupplierExpenseInfo> supplierExpenseInfos = isem.selectSupplierExpenseInfoItems(supplier_expense_ID);
        System.out.println(supplierExpenseInfos.toString());
        if (supplierExpenseInfos.isEmpty())
            return Result.failure(400, "没有数据");

//        for (SupplierExpensesItemsInfo supplierExpensesItemsInfo : supplierExpensesItemsInfos){
//            System.out.println(supplierExpensesItemsInfo);
//        }
        return Result.success(supplierExpenseInfos);
    }
}
